﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using System.IO;
using System.Data.SqlClient;

using TierApplication;


namespace BasisMgr
{
    public partial class frmRegionDetail : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _RegionID = "";
        T_Region mRegion = new T_Region();
        public bool IsPatientSearch = true;


        SqlDataAdapter sdaProductDetail = new SqlDataAdapter();
        DataSet dsProductDetail = new DataSet();
        #endregion

        public frmRegionDetail()
        {
            InitializeComponent();
        }

        private void frmRegionDetail_Load(object sender, EventArgs e)
        {
            initForm();

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    //btnSave.Enabled = false;

                    showInfo(_RegionID);
                    break;
                case Utility.OperateMode.Insert:
 
                    break;
                case Utility.OperateMode.Update:
                    txtRegionID.BackColor = Color.Silver;
                    txtRegionID.Properties.ReadOnly = true;
                    //btnSave.Enabled = true;
                    showInfo(_RegionID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }

        
        }

        private void initForm()
        {
         
        }


        private void showInfo(string sID)
        {
            mRegion.GetModel(sID);

            this.txtRegionID.Text = mRegion.RegionID;
            this.txtRegionName.Text = mRegion.RegionName;

            BindCityDetail("");
        }

        private void BindCityDetail(string bookmark)
        {

            if (_RegionID == "") return;

            SqlConnection conn = new SqlConnection(DBUtility.DbHelperSQL.connectionString);

            SqlCommand cmd;
            SqlParameter pram;
            StringBuilder strSql = new StringBuilder();

            string sSql = "";
            sSql = "select a.Region,a.CityID,a.CityName ";
            sSql = sSql + "from T_City a where a.Region ='" + _RegionID + "'";
            cmd = new SqlCommand(sSql, conn);
            sdaProductDetail.SelectCommand = cmd;

            strSql.Append("insert into T_City(");
            strSql.Append("Region,CityID,CityName)");
            strSql.Append(" values (");
            strSql.Append("@Region,@CityID,@CityName)");

            cmd = new SqlCommand(strSql.ToString(), conn);
            pram = new SqlParameter("@Region", SqlDbType.VarChar, 20);
            pram.Value = _RegionID;
            cmd.Parameters.Add(pram);
            cmd.Parameters.Add("@CityID", SqlDbType.VarChar, 20, "CityID");
            cmd.Parameters.Add("@CityName", SqlDbType.VarChar, 20, "CityName");
            sdaProductDetail.InsertCommand = cmd;

            strSql.Remove(0, strSql.Length);
            strSql.Append("update T_City set ");
            //strSql.Append("ProductID=@ProductID,");
            //strSql.Append("Region=@Region,");
            strSql.Append("CityName=@CityName");
            strSql.Append(" where CityID=@CityID ");
            cmd = new SqlCommand(strSql.ToString(), conn);
            cmd.Parameters.Add("@CityID", SqlDbType.VarChar, 20, "CityID");
            cmd.Parameters.Add("@CityName", SqlDbType.VarChar, 20, "CityName");
            sdaProductDetail.UpdateCommand = cmd;

            cmd = new SqlCommand("delete T_City where CityID=@CityID", conn);
            cmd.Parameters.Add("@CityID", SqlDbType.VarChar, 20, "CityID");
            sdaProductDetail.DeleteCommand = cmd;


            dsProductDetail.Clear();

            sdaProductDetail.Fill(dsProductDetail);
            gcGird.DataSource = dsProductDetail.Tables[0];
            //gvOrdersDetail.BestFitColumns();
        }

        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel ;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        //mProduct.CustID = Utility.GetSeqval(Utility.SeqName.CustID.ToString());
                        mRegion.Add();
                        //_ProductID = mProduct.CustID.ToString();
                        //txtProductID.Text = _ProductID;
                        _RegionID = txtRegionID.Text;
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("新增出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("商品信息新增成功!");
                    //DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                    btnCityModify_Click(null, null);

                    try
                    {
                        mRegion.RegionID = txtRegionID.Text;
                        mRegion.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("商品信息修改出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("商品信息修改成功!");
                    //DialogResult = DialogResult.OK;
                    break;
                default:
                    break;
            }
          
        }

       


        private void btnCityAdd_Click(object sender, EventArgs e)
        {
            int currentRow;
            currentRow = gvGrid.FocusedRowHandle;
            if (currentRow < 0)
                currentRow = gvGrid.GetDataRowHandleByGroupRowHandle(currentRow);
            gvGrid.AddNewRow();
            if (gvGrid.RowCount < 1) return;
            DataRow dr = gvGrid.GetDataRow(gvGrid.FocusedRowHandle);

            //dr["ProductDetailID"] = Utility.GetSeqNextval(Utility.SeqName.ProductDetailID.ToString()).ToString();
            if (gvGrid.SortInfo.GroupCount == 0) return;

            //Initialize group values
            foreach (DevExpress.XtraGrid.Columns.GridColumn groupColumn in gvGrid.GroupedColumns)
            {
                object val = gvGrid.GetRowCellValue(currentRow, groupColumn);
                gvGrid.SetRowCellValue(gvGrid.FocusedRowHandle, groupColumn, val);
            }

            gvGrid.UpdateCurrentRow();
            gvGrid.ShowEditor();
        }

        private void btnCityDelete_Click(object sender, EventArgs e)
        {
            if (gvGrid.RowCount < 1) return;

            if (MessageBox.Show("是否要删除此记录", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;


            gvGrid.DeleteRow(gvGrid.FocusedRowHandle);
        }

        private void btnCityModify_Click(object sender, EventArgs e)
        {
            gvGrid.CloseEditor();
            if (!gvGrid.UpdateCurrentRow()) return;
            try
            {
                sdaProductDetail.Update(dsProductDetail.Tables[0]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            TierService.Utility.Msg_Prompt("保存成功");
        }




    }
}